package com.star.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.star.core.base.BaseController;
import com.star.news.dto.NewsSaveDTO;
import com.star.news.dto.NewsUpdateDTO;
import com.star.news.entity.News;
import com.star.news.service.NewsService;
import com.star.news.vo.NewsDetailVO;
import com.star.news.vo.NewsDetailWxappVO;
import com.star.news.vo.NewsVO;
import com.star.security.annotations.Authorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lintianyi
 * @version 1.0
 * @date 2024/6/24 14:19
 */
@RestController
@RequestMapping("/news")
public class NewsController extends BaseController {

    @Resource
    private NewsService newsService;

    /**
     * 保存
     *
     * @param dto dto
     */
    @Authorize("news")
    @PostMapping
    public void save(@Validated @RequestBody NewsSaveDTO dto) {
        newsService.save(dto);
    }

    /**
     * 更新
     *
     * @param dto dto
     */
    @Authorize("news")
    @PutMapping
    public void update(@Validated @RequestBody NewsUpdateDTO dto) {
        newsService.update(dto);
    }

    /**
     * 更新发布和置顶状态
     *
     * @param dto dto
     */
    @Authorize("news")
    @PutMapping("/status")
    public void updateStatus(@Validated @RequestBody NewsUpdateDTO dto) {
        newsService.updateStatus(dto);
    }

    /**
     * 删除
     *
     * @param ids id
     */
    @Authorize("news")
    @DeleteMapping
    public void deleteByIds(@RequestBody List<Long> ids) {
        newsService.deleteByIds(ids);
    }

    /**
     * 列表
     *
     * @return {@link List}<{@link NewsDetailVO}>
     */
    @Authorize("news")
    @GetMapping("/page")
    public IPage<NewsVO> page(NewsUpdateDTO dto) {
        return newsService.page(dto);
    }

    /**
     * 获取详情
     *
     * @param id id
     * @return {@link IPage}<{@link NewsDetailVO}>
     */
    @Authorize("news")
    @GetMapping("/{id}")
    public NewsDetailVO getDetail(@PathVariable Long id) {
        return newsService.getDetail(id);
    }

    /**
     * 小程序获取新闻列表
     *
     * @param type
     * @param title
     * @return
     */
    @Authorize("news")
    @GetMapping("/pageWxapp")
    public List<News> pageWxapp(Integer type, String title) {
        return newsService.ListWxapp(type, title);
    }

    /**
     * 小程序获取新闻详情
     *
     * @param id
     */
    @Authorize("news")
    @GetMapping("/getDetailWxapp")
    public NewsDetailWxappVO getDetailWxapp(@PathVariable Long id) {
        return newsService.getDetailWxapp(id);
    }
}
